//
//  KeBiaoTongJiV.m
//  LoveDraw
//
//  Created by 零零壹 on 2017/7/15.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "KeBiaoTongJiV.h"
#import "KeBiaoModel.h"
#import "KeBiaoTongjiTableViewCell.h"

@implementation KeBiaoTongJiV

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(NSMutableArray *)listArr
{
    if (!_listArr) {
        _listArr = [NSMutableArray array];
    }
    return _listArr;
}

-(NSMutableDictionary *)myDic
{
    if (!_myDic) {
        _myDic = [NSMutableDictionary dictionary];
    }
    return _myDic;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];

        self.whiteV = [[UIView alloc]initWithFrame:CGRectMake(KscreenW/10, KscreenH/2-200, KscreenW*4/5, 50+3*70)];

        [self addSubview:self.whiteV];

        self.headImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KscreenW*4/5, 50)];
        self.headImageV.image = [UIImage imageNamed:@"tongjiimageV"];
        [self.whiteV addSubview:self.headImageV];

        self.headLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,KscreenW*4/5, 50)];
        [self.whiteV addSubview:self.headLabel];
        self.headLabel.text = @"统计";
        self.headLabel.textAlignment = 1;
        self.headLabel.textColor = [UIColor whiteColor];
        self.headLabel.font = [UIFont systemFontOfSize:20];


        self.listV = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, KscreenW*4/5, 70*3) style:(UITableViewStyleGrouped)];
        self.listV.delegate = self;
        self.listV.dataSource = self;
        self.listV.separatorStyle = 0;

        self.listV.estimatedSectionHeaderHeight = 0;
        self.listV.estimatedSectionHeaderHeight = 0;

        [self.listV registerNib:[UINib nibWithNibName:@"KeBiaoTongjiTableViewCell" bundle:nil] forCellReuseIdentifier:@"KeBiaoTongjiTableViewCell"];
        [self.whiteV addSubview:self.listV];
        self.listV.bounces = NO;

        UITapGestureRecognizer *hiddenTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(HiddenTap)];
        [self addGestureRecognizer:hiddenTap];

    }
    return self;
}

-(void)HiddenTap
{
    [UIView animateWithDuration:1 animations:^{
        self.alpha = 0;
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArr.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TongjiModel *model = self.listArr[indexPath.row];
    KeBiaoTongjiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KeBiaoTongjiTableViewCell" forIndexPath:indexPath];
    cell.BanjiL.text = model.coursename;
    cell.numberOfLessonL.text = model.group_nums;
    cell.moneyOfLessonL.text = model.group_fee;
    cell.selectionStyle = 0;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 70;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 70;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KscreenW*4/5, 70)];
    headV.backgroundColor = UIColors(0xEFEFEF);
    UILabel *titleL = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, (KscreenW*4/5-2)/3, 70)];
    titleL.backgroundColor = [UIColor whiteColor];
    titleL.textAlignment = 1;
    titleL.text = @"班级";
    titleL.textColor = UIColors(0x666666);

    UILabel *num = [[UILabel alloc]initWithFrame:CGRectMake((KscreenW*4/5-2)/3+1, 0, (KscreenW*4/5-2)/3, 70)];
    num.textColor = UIColors(0x666666);
    num.textAlignment = 1;
    num.backgroundColor = [UIColor whiteColor];
    num.text = @"课次";

    UILabel *money = [[UILabel alloc]initWithFrame:CGRectMake((KscreenW*4/5-2)*2/3+2, 0, (KscreenW*4/5-2)/3, 70)];
    money.text = @"课费";
    money.textColor = UIColors(0x666666);
    money.backgroundColor = [UIColor whiteColor];
    money.textAlignment = 1;

    titleL.font = [UIFont systemFontOfSize:14];
    money.font = [UIFont systemFontOfSize:14];
    num.font = [UIFont systemFontOfSize:14];

    [headV addSubview:titleL];
    [headV addSubview:num];
    [headV addSubview:money];


    return headV;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *headV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KscreenW*4/5, 70)];

    headV.backgroundColor = UIColors(0xEFEFEF);
    UILabel *titleL = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, (KscreenW*4/5-2)/3, 70)];
    titleL.backgroundColor = [UIColor whiteColor];
    titleL.textAlignment = 1;
    titleL.text = @"合计";
    titleL.textColor = UIColors(0x666666);


    UILabel *numL = [[UILabel alloc]init];
    numL.textColor = UIColors(MainColor);
    numL.textAlignment = 1;
    numL.backgroundColor = [UIColor whiteColor];
    numL.text = [NSString stringWithFormat:@"%@",self.myDic[@"total_nums"]];


    UILabel *money = [[UILabel alloc]initWithFrame:CGRectMake((KscreenW*4/5-2)*2/3+2, 0, (KscreenW*4/5-2)/3, 70)];
    money.text = [NSString stringWithFormat:@"%@元",self.myDic[@"total_fee"]];
    money.textColor = UIColors(MainColor);
    money.backgroundColor = [UIColor whiteColor];
    money.textAlignment = 1;

    titleL.font = [UIFont systemFontOfSize:14];
    money.font = [UIFont systemFontOfSize:14];
    numL.font = [UIFont systemFontOfSize:14];

    [headV addSubview:titleL];
    [headV addSubview:money];
    [headV addSubview:numL];

    [numL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleL.mas_right).offset(1);
        make.top.equalTo(headV.mas_top);
        make.bottom.equalTo(headV.mas_bottom);
        make.right.equalTo(money.mas_left).offset(-1);
    }];


    return headV;
}

-(void)apperyear:(NSString *)year month:(NSString *)month cid:(NSString *)cid
{

    [RequestManager NormalRequstWithUrl:@"/index.php?g=App&m=TeacherIndex&a=statTeacher" parmDic:@{@"tid":[[NSUserDefaults standardUserDefaults]objectForKey:@"tid"],@"year":year,@"month":month,@"cid":cid} requestType:RequestGET Success:^(id data)
    {
        self.myDic = data;
        if ([self.myDic count]==0)
        {
            [HUD showText:@"暂无课费记录" AfterTime:1];
            return ;
        }
        [UIView animateWithDuration:1 animations:^{
            self.alpha = 1;
        }];
        NSArray *dataArr=  self.myDic[@"data_list"];
        self.listArr = [TongjiModel ModelConfigure:dataArr];
        [self.listV reloadData];
    } error:^(NSError *error)
    {

    }];
}

/*
 @property(nonatomic,strong)UIView *whiteV;

 @property(nonatomic,strong)UIView *headV;

 @property(nonatomic,strong)UIView *listV;
 */


@end
